import pandas as pd
import random
import tkinter as tk
from tkinter import messagebox

# Read the Excel file
df = pd.read_excel('A1words.xlsx')
english_words = df['German'].tolist()
chinese_words = df['English'].tolist()

# Initialize variables
current_stage = 1
attempts = 0
max_attempts = 3
flashcards = []

def load_flashcards(start_index):
	global flashcards
	flashcards = list(zip(english_words[start_index:start_index+20], chinese_words[start_index:start_index+20]))
	random.shuffle(flashcards)

def show_flashcard():
	global current_word, options, attempts, flashcards

	if not flashcards:
		if attempts < max_attempts:
			attempts += 1
			load_flashcards((current_stage - 1) * 20)
			show_flashcard()
		else:
			messagebox.showinfo("Game Over", "You've completed all stages!")
			root.destroy()
		return

	current_word = flashcards.pop()
	question_label.config(text=f"Translate this word: {current_word[0]}")

	correct_option = current_word[1]
	incorrect_options = [w[1] for w in flashcards[:3]] + [correct_option]
	random.shuffle(incorrect_options)

	for i, option in enumerate(incorrect_options):
		buttons[i].config(text=option, command=lambda o=option: check_answer(o))

def check_answer(selected_option):
	global current_stage, attempts
	if selected_option == current_word[1]:
		messagebox.showinfo("Correct", "Good job!")
		if not flashcards:
			current_stage += 1
			attempts = 0
			load_flashcards((current_stage - 1) * 20)
		show_flashcard()
	else:
		messagebox.showerror("Incorrect", f"Wrong answer. The correct answer is {current_word[1]}")
		show_flashcard()

# Set up the GUI
root = tk.Tk()
root.title("Flashcard App")

question_label = tk.Label(root, text="", font=("Helvetica", 16))
question_label.pack(pady=20)

buttons = []
for i in range(4):
	button = tk.Button(root, text="", font=("Helvetica", 14), width=20)
	button.pack(pady=5)
	buttons.append(button)

load_flashcards(0)
show_flashcard()

root.mainloop()